package process;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import beans.ClientInfo;
import dao_manager.ClientInfoManagerImpl;
import dao_manager.FileMetaManager;
import dao_manager.FileMetaManagerImpl;
import dao_manager.HistoriqueFieldsManager;
import dao_manager.HistoriqueFieldsManagerImpl;

public class ProcessClientDiplay extends Action{
	
    Logger                      l                  = LoggerFactory.getLogger( ProcessClientDiplay.class );
    private static final String ATT_CLIENT         = "client";
    private static final String ATT_CLIENT_INFO_ID = "Client_info_id";
    private static final String ATT_CLIENT_HIST    = "hist_client";
    private static final String ATT_CLIENT_FILEMETA    = "file_meta";
    
	public ProcessClientDiplay()
	{
		super ();
	}

	public ActionForward execute(
	        ActionMapping mapping,
	        ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response)
	        throws Exception {

	        long id;
	        // la request.getattribute renvoie toujours null. pour controuner ca, on recupere la string query et on extrait les param dont on a besoins
	        // --------------------------------------------------------------------------------
	        try
	        {
	            String param = (String) request.getQueryString();
	            id   =  Long.parseLong( param.split( "=" )[1] ) ;
	            request.getSession().setAttribute( ATT_CLIENT_INFO_ID ,id);
	        }
	        // cas d une redirection qui ne vient pas de la page d 'accueil
	        catch( java.lang.NullPointerException e)
	        {
	            id=(Long)request.getSession().getAttribute( ATT_CLIENT_INFO_ID );
	        }
	        
	        // TODO : make cachable client_info 
	        ClientInfo clientInfo = new ClientInfoManagerImpl().findClientById( id );
	        l.debug(   clientInfo.getNomFiliale() + " a ete recupere pour affichage" );
	        l.debug( " longueur chaine commentaire " + clientInfo.getListComment().size());
	        
	      
	        
	        HistoriqueFieldsManager historiqueFieldsManager = new HistoriqueFieldsManagerImpl();
	        FileMetaManager fileMetaManager = new FileMetaManagerImpl();
	       
	        
	        request.setAttribute(ATT_CLIENT_HIST, historiqueFieldsManager.getHistorique( id ));
	        request.setAttribute(ATT_CLIENT_FILEMETA, fileMetaManager.getFileMeta( id ));
	        request.getSession().setAttribute( ATT_CLIENT, clientInfo );
	        

	        return mapping.findForward("SUCCESS");
	    }

}
